﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;

namespace RealtorsPortal.AdminCP
{
    public partial class ManageAdmins : System.Web.UI.Page
    {
        AdministratorDAL amd = new AdministratorDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAndBindData();
            }
        }

        private void LoadAndBindData()
        {
            string user = txtKeyword.Text.Trim();
            grvAdmins.DataSource = amd.GetAdmiNTableByUsername(user);
            grvAdmins.DataBind();
        }

        protected void grvAdmins_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvAdmins.EditIndex = e.NewEditIndex;
            LoadAndBindData();
        }

        protected void grvAdmins_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvAdmins.EditIndex = -1;
            LoadAndBindData();
        }

        protected void grvAdmins_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != String.Empty)
            {
                int index = Convert.ToInt32(e.CommandArgument);

                switch (e.CommandName.ToString())
                {
                    case "UpdateComponent":
                        this.UpdateAdmin(index);
                        grvAdmins.EditIndex = -1;
                        this.LoadAndBindData();
                        break;
                }
            }
        }

        private void UpdateAdmin(int index)
        {
            Administrator a = new Administrator();
            a.AdminId = int.Parse(grvAdmins.DataKeys[index].Value.ToString());
            a.Username = ((TextBox)grvAdmins.Rows[index].FindControl("etxtUsername")).Text;
            a.Email = ((TextBox)grvAdmins.Rows[index].FindControl("etxtEmail")).Text;
            a.FullName = ((TextBox)grvAdmins.Rows[index].FindControl("etxtFullname")).Text;
            amd.EditAdmin(a);
            grvAdmins.EditIndex = -1;
        }

        private void DeleteAdmin(int index)
        {
            int adminId = int.Parse(grvAdmins.DataKeys[index].Value.ToString());
            amd.DeleteAdmin(adminId);
            grvAdmins.EditIndex = -1;
        }

        protected void grvAdmins_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAdmins.PageIndex = e.NewPageIndex;
            this.LoadAndBindData();
        }

        protected void grvAdmins_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
            this.DeleteAdmin(e.RowIndex);
            grvAdmins.EditIndex = -1;
            this.LoadAndBindData();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            Administrator ad = new Administrator();
            ad.Username = txtUserName.Text;
            ad.Password = txtPassword.Text;
            ad.FullName = txtFullName.Text;
            ad.Email = txtEmail.Text;
            if (amd.AddAdmin(ad))
            {
                Response.Write(@"
                    <script type='text/javascript'>
                        alert('Added successful');
                    </script>"
                );
            }
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvAdmins.PageSize = int.Parse(ddlPageSize.Text);
            this.LoadAndBindData();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Response.Write("<script type='text/javascript'>alert('debug');</script>");
        }

        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            this.LoadAndBindData();
        }


    }
}